package com.botarena.client.battle;

import java.util.List;
import java.util.logging.Logger;

import com.botarena.client.RightMenu;
import com.botarena.client.SiteTokens;
import com.botarena.client.UIHelper;
import com.botarena.client.battle.remote.BattleAccess;
import com.botarena.client.battle.remote.BattleAccessAsync;
import com.botarena.shared.BattleInfo;
import com.botarena.shared.BattlePreviewInfo;
import com.botarena.shared.ContestInfo;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Glowna klasa GUI dla BattleService
 * 
 * @author wirenth
 * 
 */
public class BattleViewer {

	private static Logger log = Logger.getLogger(BattleViewer.class.getName());

	/**
	 * Tabela ostatnio rozgrywanych walk
	 * 
	 * @param n
	 * @param rightMenu
	 * @return
	 */
	public static Panel createLastBattlesPanel(RightMenu rightMenu) {
		final Panel panel = new VerticalPanel();
		panel.setStyleName("contentElementFull");
		
		Label title = new Label("Last fights");
		title.setStyleName("title2");
		panel.add(title);

		final BattlesPanel battlesPanel = new BattlesPanel();
		panel.add(battlesPanel);

		Anchor fights = new Anchor("All fights", SiteTokens
				.hashToken(SiteTokens.BATTLES));
		panel.add(fights);

		final ContestInfo contest = rightMenu.getSelectedContest();

		BattleAccessAsync daa = GWT.create(BattleAccess.class);
		daa.getLatestBattlesInfo(contest.getKey(), UIHelper.TOP_N,
				new AsyncCallback<List<BattleInfo>>() {

					public void onFailure(Throwable t) {
						panel.clear();
						panel
								.add(new Label(
										"Failed to load last battles info!"));
						log.severe(t.getMessage());
					}

					public void onSuccess(List<BattleInfo> bs) {

						battlesPanel.setData(bs);
						/*
						 * panel.add(new Label("Last battles: (showing " +
						 * bs.size() + " / requested " + n + ")")); for
						 * (BattleInfo b : bs) { panel.add(new
						 * Label(b.toString())); }
						 */
					}
				});

		return panel;
	}

	/**
	 * Podgląd walk
	 * 
	 * @param battleKey
	 * @return
	 */
	public static Panel createBattlePreview(String battleKey) {
		final Panel panel = new VerticalPanel();
		panel.setStyleName("contentMain");

		panel.add(UIHelper.createSmallLoader(""));

		BattleAccessAsync daa = GWT.create(BattleAccess.class);
		daa.getBattleProcess(battleKey, new AsyncCallback<BattlePreviewInfo>() {

			@Override
			public void onFailure(Throwable caught) {
				panel.clear();
				Label title = new Label("Battle preview");
				title.setStyleName("title");
				panel.add(title);
				panel.add(new Label("Failed to load battle preview!"));
				log.severe(caught.getMessage());
			}

			@Override
			public void onSuccess(BattlePreviewInfo result) {
				panel.clear();
				Label title = new Label("Battle preview");
				title.setStyleName("title");
				panel.add(title);
				if (result != null)
					panel.add(new BattlePreview(result));
				else
					panel.add(new Label("No preview"));
			}

		});

		return panel;
	}

	public static Panel createBattlePanel(RightMenu rightMenu, String botKey) {

		final Panel panel = new VerticalPanel();
		panel.setStyleName("contentMain");

		Label title = new Label("Battles");
		title.setStyleName("title");
		panel.add(title);
		
		if (rightMenu.getSelectedContest() == null) {
			panel.add(new Label("No contest selected"));
			return panel;
		}
		
		final Panel panelIn = new VerticalPanel();
		panelIn.setStyleName("rankingTable");

		final BattlesPanel battlePanel = new BattlesPanel();
		final SimplePager pager = new SimplePager();
		battlePanel.setPager(pager);
		panelIn.add(battlePanel);
		panelIn.add(pager);

		BattleAccessAsync daa = GWT.create(BattleAccess.class);
		daa.getBattles(BattlesPanel.PAGE_SIZE, rightMenu.getSelectedContest()
				.getKey(), botKey, new AsyncCallback<List<BattleInfo>>() {

			@Override
			public void onFailure(Throwable caught) {
				panelIn.clear();
				panelIn.add(new Label("Failed to load battle list!"));
				caught.printStackTrace();
			}

			@Override
			public void onSuccess(List<BattleInfo> result) {
				battlePanel.setData(result);
				pager.setPageSize(BattlesPanel.PAGE_SIZE);
				battlePanel.addRangeHandler();
			}

		});
		
		panel.add(panelIn);

		return panel;
	}

}
